package br.com.cong.view.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import br.com.cong.domain.Publicador;

public abstract class PnlCadastroPublicadorUI extends JPanel {

	private static final long serialVersionUID = 1L;

	JTextField txtNome;
	JButton btnOk;
	JButton btnCancelar;
	JButton btnPesquisar;
	private JLabel lblNome;
	private JLabel lblDataDeNascimento;
	private JLabel lblDataDeBatismo;
	private JTextField textDataNascimento;
	private JTextField txtDataBatismo;
	private JTextField txtCodigo;

	public JTextField getTextDataNascimento() {
		return textDataNascimento;
	}

	public void setTextDataNascimento(JTextField textDataNascimento) {
		this.textDataNascimento = textDataNascimento;
	}

	public JTextField getTxtDataBatismo() {
		return txtDataBatismo;
	}

	public void setTxtDataBatismo(JTextField txtDataBatismo) {
		this.txtDataBatismo = txtDataBatismo;
	}

	public JTextField getTxtCodigo() {
		return txtCodigo;
	}

	public void setTxtCodigo(JTextField txtCodigo) {
		this.txtCodigo = txtCodigo;
	}

	private JScrollPane scrollPane;
	private JTable tablePublicadores = null;
	ObjectTableModel<Publicador> tableModelPublicador;

	public PnlCadastroPublicadorUI() {
		super();
		initialize();
	}

	public JTextField getTxtNome() {
		if (txtNome == null) {
			txtNome = new JTextField();
			txtNome.setColumns(200);
		}
		return txtNome;
	}

	public JButton getBtnPesquisar() {
		if (btnPesquisar == null) {
			btnPesquisar = new JButton("Pesquisar");
			btnPesquisar.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					pesquisarPublicador();
				}
			});
		}
		return btnPesquisar;
	}

	public JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton("Salvar");
			btnOk.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					confirmarCadastroPublicador();
				}
			});
		}
		return btnOk;
	}

	public JButton getBtnCancelar() {
		if (btnCancelar == null) {
			btnCancelar = new JButton("Cancelar");
			btnCancelar.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					voltar();
				}
			});
		}
		return btnCancelar;
	}

	public void initialize() {
		setSize(415, 100);
		setPreferredSize(new Dimension(778, 398));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {95, 147, 222, 89, 78, 75, 0};
		gridBagLayout.rowHeights = new int[] {20, 0, 20, 20, 23, 23, 215, 0, 0};
		gridBagLayout.columnWeights = new double[] {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		JLabel lblCdigo = new JLabel("C\u00F3digo");
		GridBagConstraints gbc_lblCdigo = new GridBagConstraints();
		gbc_lblCdigo.anchor = GridBagConstraints.WEST;
		gbc_lblCdigo.insets = new Insets(0, 0, 5, 5);
		gbc_lblCdigo.gridx = 0;
		gbc_lblCdigo.gridy = 1;
		add(lblCdigo, gbc_lblCdigo);

		txtCodigo = new JTextField();
		GridBagConstraints gbc_txtCodigo = new GridBagConstraints();
		gbc_txtCodigo.anchor = GridBagConstraints.NORTH;
		gbc_txtCodigo.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCodigo.insets = new Insets(0, 0, 5, 5);
		gbc_txtCodigo.gridx = 1;
		gbc_txtCodigo.gridy = 1;
		add(txtCodigo, gbc_txtCodigo);
		txtCodigo.setColumns(4);
		GridBagConstraints gbc_lblNome = new GridBagConstraints();
		gbc_lblNome.anchor = GridBagConstraints.WEST;
		gbc_lblNome.insets = new Insets(0, 0, 5, 5);
		gbc_lblNome.gridx = 0;
		gbc_lblNome.gridy = 2;
		add(getLblNome(), gbc_lblNome);
		GridBagConstraints gbc_txtNome = new GridBagConstraints();
		gbc_txtNome.anchor = GridBagConstraints.SOUTH;
		gbc_txtNome.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtNome.insets = new Insets(0, 0, 5, 0);
		gbc_txtNome.gridwidth = 5;
		gbc_txtNome.gridx = 1;
		gbc_txtNome.gridy = 2;
		add(getTxtNome(), gbc_txtNome);
		GridBagConstraints gbc_lblDataDeNascimento = new GridBagConstraints();
		gbc_lblDataDeNascimento.anchor = GridBagConstraints.WEST;
		gbc_lblDataDeNascimento.insets = new Insets(0, 0, 5, 5);
		gbc_lblDataDeNascimento.gridx = 0;
		gbc_lblDataDeNascimento.gridy = 3;
		add(getLblDataDeNascimento(), gbc_lblDataDeNascimento);

		textDataNascimento = new JTextField();
		GridBagConstraints gbc_textDataNascimento = new GridBagConstraints();
		gbc_textDataNascimento.anchor = GridBagConstraints.NORTH;
		gbc_textDataNascimento.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDataNascimento.insets = new Insets(0, 0, 5, 0);
		gbc_textDataNascimento.gridwidth = 5;
		gbc_textDataNascimento.gridx = 1;
		gbc_textDataNascimento.gridy = 3;
		add(textDataNascimento, gbc_textDataNascimento);
		textDataNascimento.setColumns(10);
		GridBagConstraints gbc_lblDataDeBatismo = new GridBagConstraints();
		gbc_lblDataDeBatismo.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblDataDeBatismo.insets = new Insets(0, 0, 5, 5);
		gbc_lblDataDeBatismo.gridx = 0;
		gbc_lblDataDeBatismo.gridy = 4;
		add(getLblDataDeBatismo(), gbc_lblDataDeBatismo);

		txtDataBatismo = new JTextField();
		GridBagConstraints gbc_txtDataBatismo = new GridBagConstraints();
		gbc_txtDataBatismo.anchor = GridBagConstraints.SOUTH;
		gbc_txtDataBatismo.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDataBatismo.insets = new Insets(0, 0, 5, 0);
		gbc_txtDataBatismo.gridwidth = 5;
		gbc_txtDataBatismo.gridx = 1;
		gbc_txtDataBatismo.gridy = 4;
		add(txtDataBatismo, gbc_txtDataBatismo);
		txtDataBatismo.setColumns(10);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridheight = 2;
		gbc_scrollPane.gridwidth = 6;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridy = 5;
		gbc_scrollPane.weightx = 1.0;
		gbc_scrollPane.weighty = 1.0;
		gbc_scrollPane.insets = new Insets(5, 0, 5, 5);
		gbc_scrollPane.gridx = 0;
		add(getScrollPane(), gbc_scrollPane);

		GridBagConstraints gbc_btnPesquisar = new GridBagConstraints();
		gbc_btnPesquisar.anchor = GridBagConstraints.NORTH;
		gbc_btnPesquisar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnPesquisar.insets = new Insets(0, 0, 0, 5);
		gbc_btnPesquisar.gridx = 3;
		gbc_btnPesquisar.gridy = 7;
		add(getBtnPesquisar(), gbc_btnPesquisar);
		GridBagConstraints gbc_btnOk = new GridBagConstraints();
		gbc_btnOk.anchor = GridBagConstraints.NORTH;
		gbc_btnOk.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnOk.insets = new Insets(0, 0, 0, 5);
		gbc_btnOk.gridx = 4;
		gbc_btnOk.gridy = 7;
		add(getBtnOk(), gbc_btnOk);
		GridBagConstraints gbc_btnCancelar = new GridBagConstraints();
		gbc_btnCancelar.anchor = GridBagConstraints.NORTHWEST;
		gbc_btnCancelar.gridx = 5;
		gbc_btnCancelar.gridy = 7;
		add(getBtnCancelar(), gbc_btnCancelar);
	}

	private JLabel getLblNome() {
		if (lblNome == null) {
			lblNome = new JLabel("Nome");
		}
		return lblNome;
	}

	protected JLabel getLblDataDeNascimento() {
		if (lblDataDeNascimento == null) {
			lblDataDeNascimento = new JLabel("Data de nascimento");
		}
		return lblDataDeNascimento;
	}

	protected JLabel getLblDataDeBatismo() {
		if (lblDataDeBatismo == null) {
			lblDataDeBatismo = new JLabel("Data de batismo");
		}
		return lblDataDeBatismo;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.getVerticalScrollBar().setAutoscrolls(true);
			scrollPane.setAutoscrolls(true);
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			scrollPane.setViewportView(getTablePublicadores());
		}
		return scrollPane;
	}

	protected ObjectTableModel<Publicador> getTablePublicadorModel() {
		if (this.tableModelPublicador == null) {
			this.tableModelPublicador = new ObjectTableModel<Publicador>();
			this.tableModelPublicador.addColumn("codigo", "id");
			this.tableModelPublicador.addColumn("Nome", "nome");
		}
		return this.tableModelPublicador;
	}

	protected JTable getTablePublicadores() {
		if (this.tablePublicadores == null) {
			this.tablePublicadores = new JTable();
			this.tablePublicadores.setModel(getTablePublicadorModel());
			this.tablePublicadores.getColumnModel().getColumn(0).setPreferredWidth(80); // numeroPedido
			this.tablePublicadores.getColumnModel().getColumn(1).setPreferredWidth(200); // numeroLoja
			this.tablePublicadores.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			this.tablePublicadores.setRowHeight(22);
			this.tablePublicadores.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						selecionarPublicador();
					}
				}
			});
			this.tablePublicadores.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		return this.tablePublicadores;
	}

	protected abstract void confirmarCadastroPublicador();

	protected abstract void voltar();

	protected abstract void selecionarPublicador();

	protected abstract void pesquisarPublicador();
}
